package com.hiwetop.cp;

import com.fj.hiwetoptools.RandomGenerator;
import com.fj.hiwetoptools.StringUtil;
import com.fj.hiwetoptools.system.QRCodeFrame;
import com.fj.hiwetoptools.web.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.HttpCookie;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Created by linyu on 2016/12/22.
 */
public class Vote {
    public static VoteConnectionExample http = new VoteConnectionExample();
    public static RandomGenerator random = new RandomGenerator();

    public static String randomCookies(){
        String cookies = "province=%E7%A6%8F%E5%BB%BA;city=%E7%A6%8F%E5%B7%9E;country=%E4%B8%AD%E5%9B%BD;openid={};nickname={};PHPSESSID={};sex=1";
        String openid = random.nextLetter(28);
        String nickName = random.nextLetter(8);
        String PHPSESSID = random.nextLetter(26);
        cookies = StringUtil.format(cookies,openid,nickName,PHPSESSID);
        return cookies;
    }

    public static void doMain()throws  Exception{
        String cookies = randomCookies();
        String verify = " http://wx.e7wei.net/index/verify.html";
        String result = http.GetPageContent(verify,cookies);
        Thread.sleep(2000);
        InputStream bis = new FileInputStream(new File("code.jpg"));
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        int i;
        while((i=bis.read())!=-1){
            baos.write(i);
        }

        bis.close();
        JFrame jf=new JFrame("图片显示窗体");
        jf.setBounds(30, 30, 500, 500);
        JPanel panel = new JPanel();
        panel.setBounds(30, 30, 300, 300);
        //通过ImageIcon将字节数组组解码为图片，放入JLabel组件中
        JLabel labImg=new JLabel(new ImageIcon(baos.toByteArray()));
        panel.add(labImg);
        JPanel panel1 = new JPanel();
        JTextField tx = new JTextField(10);
        JButton btn = new JButton("确定");
        JLabel label = new JLabel("结果");
        btn.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        System.out.println(tx.getText());
                        try {
                            String result = doPost(tx.getText(),cookies);
                            label.setText(result);
                            jf.setVisible(false);
                            doMain();
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                });
        panel1.add(btn);
        panel1.add(tx);
        panel1.add(label);
        jf.add(panel);
        jf.add(panel1);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
       doMain();


    }

    public static String doPost(String code,String cookies) throws Exception {
        String url = "http://wx.e7wei.net/index/vote.html";
//        String postParams = "verify=dyhx&id=4051&vid=465&uid=141950";
        String postParams = "verify="+code+"&id=4051&vid=465&uid=141950";
        return http.sendPost(url, postParams,"http://wx.e7wei.net/index/detail/vid/465/uid/4051.html",cookies);
    }
}
